import Scanner from './scanner';
import nestTokens from "./nestToken";

export default function parseTemplateToToken(templateStr) {
    const token = [];
    const scanner = new Scanner(templateStr);
    let word;

    while (scanner.eos()) {
        word = scanner.scanUtil('{{');
        scanner.scan('{{');
        if (word != '') {
            token.push(['text', word]);
        }

        word = scanner.scanUtil('}}');
        scanner.scan('}}');
        if (word != '') {
            if (word[0] === '#') {
                token.push(['#', word.substring(1)]);
            } else if (word[0] === '/') {
                token.push(['/', word.substring(1)]);
            } else {
                token.push(['name', word]);
            }
        }
    }

    return nestTokens(token);
};